package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.MyToast;
import com.jbm.jbmsupplier.util.IVerifyCode;
import com.jbm.jbmsupplier.util.Login;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.example.jbmsuppliers.R.id.header_right;

/**
 * 李征
 * Created by 家帮忙 on 2017/1/11.
 */

public class RegisterActivity extends OvertActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(header_right)
    TextView headerRight;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.weinxin_image)
    ImageView weinxinImage;
    @InjectView(R.id.phones)
    EditText phones;
    @InjectView(R.id.identifyings)
    Button identifyings;
    @InjectView(R.id.authcodes)
    EditText authcodes;
    @InjectView(R.id.agreements)
    CheckBox agreements;
    @InjectView(R.id.re_phone_xys)
    Button rePhoneXys;
    @InjectView(R.id.procode)
    EditText procode;
    private MyCount mcount;
    public final static int WX_CODE_VERS = 0001;
    public final static int WX_BINGS = 0002;
    private SharedPreferences mSharedPreferences;
    private DialogLoading mDialog;
    private boolean IsPitch = true;
    private SharedPreferences.Editor editshar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wx_register);
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        mSharedPreferences = getSharedPreferences("sinopecall", Activity.MODE_PRIVATE);
        editshar = mSharedPreferences.edit();
        headerRight.setText("绑定");
        headerTitle.setText("绑定");
    }


    //获取验证码
    public void getGainver() {
        if (IsPitch)
            if (phones.getText().toString().trim().equals("")) {
                //微信客户端登录获得手机号
                phones.setFocusable(true);
                MyToast.show(RegisterActivity.this, "电话号码不能为空！", Toast.LENGTH_SHORT);
            }
        if (phones.getText().toString().length() > 11 || phones.getText().toString().length() < 11) {
            phones.setFocusable(true);
            MyToast.show(RegisterActivity.this, "请检查手机号长度！", Toast.LENGTH_SHORT);
        } else {
            mcount = new MyCount(90000, 1000);
            mcount.start();
            NetWork network = new NetWork(handler);
            network.getGainvers(phones.getText().toString());
            mDialog = new DialogLoading(this, "验证中...", false);
            mDialog.show();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case WX_CODE_VERS: {
                    IVerifyCode result = (IVerifyCode) msg.obj;
                    if (result != null) {
                        if (result.back) {

                            //获取验证码成功
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                        } else {
                            //获取验证码失败
                            if (mcount != null) {
                                identifyings.setText("点击获取验证码");
                                mcount.cancel();
                            }
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            hint(result.errorinfo);

                        }
                    }

                    break;
                }
                case WX_BINGS: {
                    Login result = (Login) msg.obj;
                    if (result != null) {
                        if (result.login_id > 1) {
                            if (mcount != null) {
                                identifyings.setText("点击获取验证码");
                                mcount.cancel();
                            }
                            //帮定成功
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }editshar .putString("weinxinlogin", "weixin")
                         .putString("phname",mSharedPreferences.getString("phname",""))
                          .putBoolean("IsLogin", true).commit();
                           /* .putInt("result.login_id",result.login_id).commit();*/
                            sendBroadcast(new Intent("isBack").putExtra("login",false).putExtra("relogin","login"));//
                            MainActivity.goNewHome();
                            finish();
                        } else if (result.equals("time out")) {
                            if (mcount != null) {
                                identifyings.setText("点击获取验证码");
                                mcount.cancel();
                            }
                            //绑定超时
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            hint(result.errorinfo);
                        } else {
                            if (mcount != null) {
                                identifyings.setText("点击获取验证码");
                                mcount.cancel();
                            }
                            //错误
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            hint(result.errorinfo);
                        }


                    }


                    break;
                }

            }


        }
    };


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            IsPitch = true;
            headerRight.setTextColor(Color.WHITE);
            identifyings.setTextColor(Color.WHITE);
        } else {
            IsPitch = false;
            headerRight.setTextColor(Color.DKGRAY);
            identifyings.setTextColor(Color.DKGRAY);
        }

    }

    @OnClick({header_right, R.id.header_back, R.id.re_phone_xys, R.id.identifyings})
    public void onClick(View view) {
        switch (view.getId()) {
            case header_right: //绑定操作
                if (IsPitch)
                    if (phones.getText().toString().trim().equals("")) {
                        //微信客户端登录获得手机号
                        phones.setFocusable(true);
                        MyToast.show(RegisterActivity.this, "电话号码不能为空！", Toast.LENGTH_SHORT);
                    }
                if (phones.getText().toString().length() > 11 || phones.getText().toString().length() < 11) {
                    phones.setFocusable(true);
                    MyToast.show(RegisterActivity.this, "请检查手机号长度！", Toast.LENGTH_SHORT);
                } else if (authcodes.getText().toString().equals("")) {
                    phones.setFocusable(true);
                    MyToast.show(RegisterActivity.this, "验证码不能为空！", Toast.LENGTH_SHORT);
                } else {
                    String username = mSharedPreferences.getString("phname", "");//登录id
                    String nickname = mSharedPreferences.getString("username", "");//昵称
                    int sex = mSharedPreferences.getInt("sex", 0);//性别
                    String headimgurl = mSharedPreferences.getString("headimgurl", "");//图片
                    NetWork network = new NetWork(handler);
                    network.getLoginCoss(phones.getText().toString(), authcodes.getText().toString(), username, nickname, sex, headimgurl,procode.getText().toString());
                    mDialog = new DialogLoading(this, "验证中...", false);
                    mDialog.show();

                }
                break;
            case R.id.header_back:
                finish();
                break;
            case R.id.re_phone_xys:

                startActivity(new Intent(RegisterActivity.this, MyAgreeManage.class));

                break;
            case R.id.identifyings:
                getGainver();
                break;

        }
    }


    /*定义一个倒计时的内部类*/
    class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            identifyings.setText("发送验证码");

        }

        @Override
        public void onTick(long millisUntilFinished) {
            identifyings.setText("请等待90秒(" + millisUntilFinished / 1000 + ")...");

        }
    }


}
